package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.netEase.dao.replyDao;
import org.netEase.entity.reply;
import org.netEase.util.BaseDao;

public class replyDaoImpl extends BaseDao implements replyDao {

	@Override
	public int add(reply re) {
		 String sql = "insert into reply(hid,coid,rtext)values(?,?,?)";	
			return super.executeUpdate(sql,re.getHid(),re.getCoid(),re.getRtext());
	}
	/*public static void main (String [] args) {
		reply er= new reply();
		er.setHid(Integer.parseInt("1"));
		er.setCoid(Integer.parseInt("2"));
		er.setRtext("asdfsadfsdf");
		replyDao reDao=new replyDaoImpl();
		int re=reDao.add(er);
		System.out.println(re);
	}*/

	@Override
	public List<reply> replies(int coid) {
		List<reply> list = new ArrayList<>();
		String sql = "SELECT * FROM reply r INNER JOIN zhanghu z ON r.hid = z.hid WHERE r.coid = ?";
		ResultSet rs = super.executeQuery(sql, coid);
		try {
			while (rs.next()) {
				reply reply = new reply();
				reply.setCoid(rs.getInt("coid"));
				reply.setDateday(rs.getTimestamp("dateday"));
				reply.setHid(rs.getInt("hid"));
				reply.setHnamesp(rs.getString("namesp"));
				reply.setReid(rs.getInt("reid"));
				reply.setRtext(rs.getString("rtext"));
				
				list.add(reply);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
/*	public static void main(String[] args) {
		replyDao replyDao = new replyDaoImpl();
		List<reply> list=replyDao.replies(14);
		for (reply reply : list) {
			System.out.println(reply.getHnamesp());
		}
	}*/

	@Override
	public List<Integer> ridByhid(int hid) {
		String sql = "SELECT reid FROM reply WHERE hid = ?";
		List<Integer> list = new ArrayList<>();
		ResultSet rs = super.executeQuery(sql, hid);
		try {
			while(rs.next()) {
				int a = 0;
				a = rs.getInt("reid");
				
				list.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
/*	public static void main(String[] args) {
		replyDao replyDao = new replyDaoImpl();
		List<Integer> list =replyDao.ridByhid(2);
		for (Integer integer : list) {
			System.out.println(integer);
		}
	}*/
/*	public static void main(String[] args) {
		replyDao replyDao2 = new replyDaoImpl();
		StringBuffer sb = new StringBuffer();
		List<Integer> list2=replyDao2.ridByhid(Integer.parseInt("2"));
		for (Integer integer : list2) {
			sb.append(integer);
			sb.append(",");
		}
		sb.deleteCharAt(sb.length()-1);
	
	}*/
}
